package com.ruowei.modules.sys.utils;


import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 各种时间之间的转换工具
 *
 * @author 董兴
 * @date 2019年8月22日
 */
public class DateConvertUtils {

    /**
     * LocalDate转Date
     *
     * @param localDate
     * @return
     */
    public static Date localdateToDate(LocalDate localDate) {
        if (null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * Date转LocalDate
     *
     * @param date
     * @return
     */
    public static LocalDate dateToLocalDate(Date date) {
        if (null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * @param date
     * @apiNote Date 转 ZonedDateTime
     * @author 董兴
     * @date 2020-01-15
     */
    public static ZonedDateTime dateToZonedDateTime(Date date) {
        if (date == null) {
            return null;
        }
        return ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    public static Date zonedDateTimeToDate(ZonedDateTime time) {
        if (time == null) {
            return null;
        }
        return localdateToDate(time.toLocalDate());
    }

    /**
     * String 转 LocalTime
     *
     * @param str
     * @return
     * @author 董兴
     * @date 2019年8月27日
     */
    public static LocalTime stringToLocalTime(String str) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("HH:mm");
        return LocalTime.parse(str, df);
    }

    /**
     * Instant 转 String
     *
     * @param instant
     * @author 刘东奇
     * @date 2019/9/16
     */
    public static String InstantToString(Instant instant) {
        if (instant != null) {
            Date tmpDate = Date.from(instant);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String result = formatter.format(tmpDate);
            return result;
        } else {
            return "";
        }
    }

    /**
     * @param instant
     * @apiNote 返回Instant
     * @author 董兴
     * @date 2019-12-17
     */
    public static String InstantToLocalTime(Instant instant) {
        if (instant != null) {
            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            return (localDateTime.getHour() < 10 ? "0" + localDateTime.getHour() : String.valueOf(localDateTime.getHour())) + ":"
                + (localDateTime.getMinute() < 10 ? "0" + localDateTime.getMinute() : String.valueOf(localDateTime.getMinute()));
        } else {
            return "";
        }
    }

    /**
     * String 转 LocalDate
     *
     * @param str
     * @author 董兴
     * @date 2019-09-16
     */
    public static LocalDate stringToLocalDate(String str) {
        return LocalDate.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public static String instanceToLocalDate(String str) {
        Instant instant;
        if (StringUtils.isNotEmpty(str)) {
            instant = Instant.parse(str);
            return instantToLocalDate(instant).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        } else {
            return "";
        }
    }

    /**
     * UTC时间格式  获取两者时间差天数
     *
     * @param startTimeStr 开始时间
     * @param endTimeStr   结束时间
     */
    public static Integer calculateDeductionDays(String startTimeStr, String endTimeStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        Date startTime = null;
        Date endTime = null;
        try {
            startTime = simpleDateFormat.parse(startTimeStr);
            endTime = simpleDateFormat.parse(endTimeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Integer days = (int) ((endTime.getTime() - startTime.getTime()) / (1000 * 3600 * 24));
        return days;
    }

    /**
     * @param localDate
     * @param localTimeString
     * @apiNote LocalDate 和 LocalTime结合转Instant
     * @author 董兴
     * @date 2019-12-17
     */
    public static Instant localDateAndLocalTimeToInstant(LocalDate localDate, String localTimeString) {
        LocalTime localTime = stringToLocalTime(localTimeString);
        LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant();
    }

    // 获得某天最大时间 2017-10-15 23:59:59
    public static Instant getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return endOfDay.atZone(ZoneId.systemDefault()).toInstant();
    }

    // 获得某天最小时间 2017-10-15 00:00:00
    public static Instant getStartOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return startOfDay.atZone(ZoneId.systemDefault()).toInstant();
    }

    /**
     * @param instant
     * @apiNote Instant 转 LocalDate
     * @author 董兴
     * @date 2019-12-23
     */
    public static LocalDate instantToLocalDate(Instant instant) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return localDateTime.toLocalDate();
    }
}
